var WFClientLogger = new Class ({    
    act: "writeClientLog",
    sessionId: Cookie.get("PHPSESSID",{path: wf_cookie_path}),
    userId: Cookie.get("userID",{path: wf_cookie_path}),
    browserVersion: Browser.name + " " + Browser.version,
    message: null,    
    path: null,
    lineNumber: null,
    stack: null,
    initialize: function(){
        if(wfconf_client_debug_level != "OFF"){
            this.clientLogger = log4javascript.getLogger();
            this.ajaxAppender = new log4javascript.AjaxAppender(wfc_servercgi_other_controller);
            if (wfconf_client_debug_level == "INFO"){
                this.ajaxAppender.setThreshold(log4javascript.Level.INFO);            
            }else if (wfconf_client_debug_level == "FATAL"){
                this.ajaxAppender.setThreshold(log4javascript.Level.FATAL);
            }else if (wfconf_client_debug_level == "ERROR"){
                this.ajaxAppender.setThreshold(log4javascript.Level.ERROR);
            }else if (wfconf_client_debug_level == "WARN"){
                this.ajaxAppender.setThreshold(log4javascript.Level.WARN);
            }else if (wfconf_client_debug_level == "DEBUG"){
                this.ajaxAppender.setThreshold(log4javascript.Level.DEBUG);
            }else{
                this.ajaxAppender.setThreshold(log4javascript.Level.OFF);
            }

            this.ajaxAppender.getLayout().setCustomField("act", "writeClientLog");
            this.clientLogger.addAppender(this.ajaxAppender);

            if (wfconf_client_debug_console && wfconf_client_debug_console_debugger_userIds.contains(this.userId)){
                var popUpAppender = new log4javascript.PopUpAppender();
                var popUpLayout = new log4javascript.PatternLayout("%d,%p,%m%n");
                popUpAppender.setLayout(popUpLayout);
                this.clientLogger.addAppender(popUpAppender);
            }
        }
    },
    fatalWithException: function(e, stack){
        if(this.clientLogger.isFatalEnabled){
            this.prepareErrorMessage(e, stack);
            this.clientLogger.fatal(this.getFormatedLog());
        }
    },
    fatalWithMessage: function(message, stack){
        if(this.clientLogger.isFatalEnabled){
            this.message = message;
            this.path = null;
            this.lineNumber = null;
            this.stack = this.getStackWithinLimit(stack);
            this.clientLogger.fatal(this.getFormatedLog());
        }
    },
    errorWithException: function(e, stack){       
        if(this.clientLogger.isErrorEnabled){
            this.prepareErrorMessage(e, stack);
            this.clientLogger.error(this.getFormatedLog());
        }
    },
    errorWithMessage: function(message, stack){
        if(this.clientLogger.isErrorEnabled){
            this.message = message;
            this.path = null;
            this.lineNumber = null;
            this.stack = this.getStackWithinLimit(stack);
            this.clientLogger.error(this.getFormatedLog());
        }
    },
    //only for IE and firefox
    windowError: function(message, url, line, stack){
        if(this.clientLogger.isErrorEnabled){
            this.message = message;
            this.path = url;
            this.lineNumber = line;
            this.stack = this.getStackWithinLimit(stack);
            this.clientLogger.error(this.getFormatedLog());
        }
    },
    warn: function(message){
        if(this.clientLogger.isWarnEnabled){
            this.message = message;
            this.path = null;
            this.lineNumber = null;
            this.stack = null;
            this.clientLogger.warn(this.getFormatedLog());
        }
    },
    info: function(message){
        if(this.clientLogger.isInfoEnabled){
            this.message = message;
            this.path = null;
            this.lineNumber = null;
            this.stack = null;
            this.clientLogger.info(this.getFormatedLog());
        }
    },
    debug: function(message, stack){
        if(this.clientLogger.isDebugEnabled){
            this.message = message;
            this.path = null;
            this.lineNumber = null;
            this.stack = this.getStackWithinLimit(stack);
            this.clientLogger.debug(this.getFormatedLog());
        }
    },
    getStackWithinLimit: function(stack){
        if (stack != null){
            if (stack.length > wfconf_client_debug_max_stack_size){
                stack = stack.substring(0, wfconf_client_debug_max_stack_size);
            }
        }
        return stack;
    },
    getFormatedLog: function(){
        return this.sessionId + ","+this.userId+","+this.browserVersion+","+this.message+",***("+this.path+":"+this.lineNumber+")***["+this.stack+"]";
    },
    prepareErrorMessage: function(e, stack){
        this.message = e.message;
        if(Browser.ie){
            this.path = null;
            this.lineNumber = e.number;
        }else if (Browser.firefox){
            this.path = e.fileName;
            this.lineNumber = e.lineNumber;
        }else{
            this.path = null;
            this.lineNumber = null;
        }
        this.stack = this.getStackWithinLimit(stack);
    }
});
var clientLogger = new WFClientLogger();